<?php

class Recursos_Model_PermissaoMapper
{
	protected $_dbTable;
	
	public function setDbTable($dbTable) {
		if (is_string ( $dbTable )) {
			$dbTable = new $dbTable ();
		}
		if (! $dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception ( 'Invalid table data gateway provided' );
		}
		$this->_dbTable = $dbTable;
		return $this;
	}
	public function getDbTable() {
		if (null === $this->_dbTable) {
			$this->setDbTable ( 'Recursos_Model_DbTable_Permissao' );
		}
		return $this->_dbTable;
	}
	
	private function _fillPermissao($row){
		$permissao = new Recursos_Model_Permissao();
		$permissao->setId($row->id)
		->setId_perfil($row->id_perfil)
		->setId_recurso($row->id_recurso)
		->setPermissao($row->permissao);
		return $permissao;
	}
	
		
	public function findpopule($id)
	{
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		return $data = array(
				"id" => $row->id,
				"id_perfil" => $row->id_perfil,
				"id_recusro" => $row->id_recurso,
				"permissao" => $row->permissao
		);
	}
	
	public function findid($id_recurso, $id_perfil)
	{
		$sql = $this->getDbTable()->select()
				->where('id_recurso = ?', $id_recurso)
				->where('id_perfil = ?', $id_perfil)
				; 
		$result = $this->getDbTable()->fetchRow($sql);
		if($result === NULL) return NULL;
		$row = $this->_fillPermissao($result);
		return $row;
	}
	
	public function findperfil($id_perfil)
	{
		$sql = $this->getDbTable()->select()
		->where('id_perfil = ?', $id_perfil);
		$resultSet = $this->getDbTable()->fetchAll($sql);
		$permissao = array();
		if ($resultSet->count () > 0) {
			foreach ( $resultSet as $row ) {
				$perfil = $row->findParentRow('Usuarios_Model_DbTable_Perfil',"Perfil");
				$recurso = $row->findParentRow('Recursos_Model_DbTable_Recursos',"Recursos");
				$permissao[] = array('perfil' => $perfil, 'recurso'	=> $recurso, 'permissao'=>$row);
			}
			return $permissao;
		}
	}
	
	public function fetchAll() {
		$sql = $this->getDbTable ()->select ()->order('id_recurso');
		$resultSet = $this->getDbTable ()->fetchAll ( $sql );
		$entries = array ();
		if ($resultSet->count () > 0) {
			foreach ( $resultSet as $row ) {
				$entries [] = $this->_fillPermissao ( $row );
			}
		}
		return $entries;
	}
	
	private function _editPermission($id_recurso, $id_perfil, $allow){	
		$sql = $this->getDbTable()->select()
		->where('id_recurso = ?', $id_recurso)
		->where('id_perfil = ?', $id_perfil);
		$row = $this->getDbTable()->fetchRow($sql);
		$data = array('id_recurso' => $id_recurso, 'id_perfil' => $id_perfil, 'permissao' => $allow);
		if($row){ 
			#atualizar
			$this->getDbTable ()->update ( array('permissao' => $allow), array ('id = ?' => $row->id));
		}else { 
			#salvar 
			$this->getDbTable ()->insert ( $data );
		}
	}
	
	public function editPermission(Recursos_Model_Permissao $permissao, $data){
		$modelperfil = new Usuarios_Model_PerfilMapper();
		$perfils = $modelperfil->listPerfils(true);
		foreach($data as $id_recurso => $recurso){
			if(!is_numeric($id_recurso)) continue;
			foreach ($perfils as $id_perfil => $name){
				if (isset($recurso['perfil'])){
					$perfil = (in_array($id_perfil,$recurso['perfil']))? 'allow': 'deny';
				}else{
					#
					$perfil = 'deny';
				}
				$this->_editPermission($id_recurso, $id_perfil, $perfil);
			}
								
			
		}
	}
	###############  Modos de listagem ####################
	
	public function listPermissao(){
	
		$recursos = new Recursos_Model_RecursosMapper();
		$sql = $recursos->getDbTable()->select()->order(array("recurso ASC"));
		$resultSet = $recursos->getDbTable()->fetchAll($sql);
		$entries   = array();
				
		foreach ($resultSet as $row) {
			$entries[] = array('recurso' => $row, 'perfil' => $this->loadperfil($row));
		}
		return $entries;
	}
	
	private function loadperfil(Zend_Db_Table_Row $row){
		$sql = $this->getDbTable()->select()->where('permissao = "allow"');
		$rowsetPermissao = $row->findManyToManyRowset ( 'Usuarios_Model_DbTable_Perfil', new Recursos_Model_DbTable_Permissao(), null,null, $sql );
		return $rowsetPermissao;
	}
	
}

